package com.nsu.evaluation.service.impl;

import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.dao.CourseMapper;
import com.nsu.evaluation.pojo.Course;
import com.nsu.evaluation.pojo.CourseExample;
import com.nsu.evaluation.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService{

    @Autowired
    private CourseMapper courseMapper;

    /**
     * 新增课程信息
     * @return
     */
    @Override
    public ServerResponse<Course> addCourse(Course course){
        int rowCount = courseMapper.insert(course);
        if (rowCount > 0) {
            return ServerResponse.createBySuccess("新增课程成功",course);
        }
        return ServerResponse.createByErrorMessage("新增课程失败");
    }

    /**
     * 删除课程信息
     * @param courseId
     * @return
     */
    @Override
    public ServerResponse deleteCourseByPrimaryKey(String courseId){
        int rowCount = courseMapper.deleteByPrimaryKey(courseId);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("删除课程成功",courseId);
        }
        return ServerResponse.createByErrorMessage("删除课程失败");
    }

    /**
     * 修改课程信息
     * @param course
     * @return
     */
    @Override
    public ServerResponse<Course> updateCourseByPrimaryKey(Course course){
        int rowCount = courseMapper.updateByPrimaryKey(course);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("修改课程成功",course);
        }
        return ServerResponse.createByErrorMessage("修改课程失败");
    }

    /**
     * 根据课程名称或科任老师查询课程信息
     * @param course
     * @return
     */
    @Override
    public ServerResponse<List<Course>> selectCourseInfoByExample(Course course){
        CourseExample courseExample = new CourseExample();
        CourseExample.Criteria criteria = courseExample.createCriteria();
        if(null != course.getCourseName() && !course.getCourseName().equals("")){
            criteria.andCourseNameEqualTo(course.getCourseName());
        }
        if(null != course.getCreateUser() && !course.getCreateUser().equals("")){
            criteria.andCreateUserEqualTo(course.getCreateUser());
        }
        List<Course> coursesList = courseMapper.selectByExample(courseExample);
        return ServerResponse.createBySuccess("查询课程成功",coursesList);
    }

    /**
     * 获取课程列表
     * @return
     */
    @Override
    public ServerResponse<List<Course>> selectCourseInfoList(){
        CourseExample courseExample = new CourseExample();
        List<Course> courseList = courseMapper.selectByExample(courseExample);
        return ServerResponse.createBySuccess("获取列表成功",courseList);
    }
}
